#!/bin/bash
set -e

# Default values
BUILD_DIR="build"
ENABLE_CUDA=OFF
BUILD_TESTING=OFF
INSTALL=false
CLEAN=false
CMAKE_INSTALL_PREFIX="/usr/local"
CUDA_ARCH="89"

# Display help
show_help() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo "  -h, --help                 Show this help message"
    echo "  --clean                    Clean build directory before building"
    echo "  --cuda                     Enable CUDA support"
    echo "  --cuda-arch ARCH           Set CUDA architecture (default: 89, can be multiple values like '70;75')"
    echo "  --test                     Enable testing"
    echo "  --install                  Install after building"
    echo "  --prefix PATH              Set installation prefix (default: /usr/local)"
    echo "  --build-dir PATH           Set build directory (default: build)"
    echo ""
    echo "Example: $0 --cuda --cuda-arch \"70;75\" --test --install --prefix ~/local"
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        --clean)
            CLEAN=true
            shift
            ;;
        --cuda)
            ENABLE_CUDA=ON
            shift
            ;;
        --cuda-arch)
            CUDA_ARCH="$2"
            shift 2
            ;;
        --test)
            BUILD_TESTING=ON
            shift
            ;;
        --install)
            INSTALL=true
            shift
            ;;
        --prefix)
            CMAKE_INSTALL_PREFIX="$2"
            shift 2
            ;;
        --build-dir)
            BUILD_DIR="$2"
            shift 2
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

# Clean build directory if requested
if [ "$CLEAN" = true ] && [ -d "$BUILD_DIR" ]; then
    echo "Cleaning build directory..."
    rm -rf "$BUILD_DIR"
fi

# Create build directory if it doesn't exist
if [ ! -d "$BUILD_DIR" ]; then
    mkdir -p "$BUILD_DIR"
fi

# Enter build directory
cd "$BUILD_DIR"

# Set compiler environment variables
export CC=mpicc
export FC=mpif90

# Configure with CMake
echo "Configuring project..."
cmake_args="-DBUILD_TESTING=$BUILD_TESTING -DENABLE_CUDA=$ENABLE_CUDA -DCMAKE_INSTALL_PREFIX=$CMAKE_INSTALL_PREFIX"

# Add CUDA architecture if CUDA is enabled
if [ "$ENABLE_CUDA" = "ON" ]; then
    cmake_args="$cmake_args -DCMAKE_CUDA_ARCHITECTURES=$CUDA_ARCH"
fi

cmake .. $cmake_args

# Build
echo "Building project..."
cmake --build . -- -j$(nproc)

# Run tests if enabled
if [ "$BUILD_TESTING" = "ON" ]; then
    echo "Running tests..."
    ctest
fi

# Install if requested
if [ "$INSTALL" = true ]; then
    echo "Installing to $CMAKE_INSTALL_PREFIX..."
    if [ -w "$CMAKE_INSTALL_PREFIX" ]; then
        cmake --install .
    else
        echo "Insufficient permissions to install to $CMAKE_INSTALL_PREFIX. Using sudo..."
        sudo cmake --install .
    fi
fi

echo "Build completed successfully!"

# Display summary
echo ""
echo "Build Summary:"
echo "  CUDA Support: $ENABLE_CUDA"
if [ "$ENABLE_CUDA" = "ON" ]; then
    echo "  CUDA Architecture: $CUDA_ARCH"
fi
echo "  Tests Enabled: $BUILD_TESTING"
echo "  Installation Prefix: $CMAKE_INSTALL_PREFIX"
echo ""
echo "To use the libraries:"
echo "  - C library: -lcdebug_utils"
if [ "$ENABLE_CUDA" = "ON" ]; then
    echo "  - CUDA library: -lcudadebug_utils"
fi
echo "  - Fortran library: -lfdebug_utils"
echo ""
echo "For more details, see the README.md file."

# Ensure the comparison script exists
ROOT_DIR=".."  # Path to project root from build directory

# Create necessary directories if they don't exist
mkdir -p "$ROOT_DIR/compare-diff/sh" 2>/dev/null || true

if [ -f "$ROOT_DIR/compare_norm_simple.sh" ]; then
    echo "Found compare_norm_simple.sh in root directory"
    # Copy to the expected directory for consistency
    if [ ! -f "$ROOT_DIR/compare-diff/sh/compare_norm_simple.sh" ]; then
        echo "Copying comparison script to compare-diff/sh directory for consistency..."
        cp "$ROOT_DIR/compare_norm_simple.sh" "$ROOT_DIR/compare-diff/sh/"
    fi
else
    script_path="$ROOT_DIR/compare-diff/sh/compare_norm_simple.sh"
    if [ -f "$script_path" ]; then
        echo "Copying comparison script to root directory..."
        cp "$script_path" "$ROOT_DIR/"
        chmod +x "$ROOT_DIR/compare_norm_simple.sh"
    else
        echo "Warning: Could not find comparison script at $script_path or in root directory"
    fi
fi 